import { getBusinessParamsList, getBusinessParamsById, addBusinessParams, updateBusinessParams, deleteBusinessParamsById, getAllBusinessParamCode } from '../../services/basedata/businessParams';
import { parse } from 'qs';
import { message } from 'antd';
import Constants from '../../utils/Constants';
const R = require('ramda');
export default {
    namespace: 'businessParams',
    state: {
        search: { // 查询条件
            currencyCode: '',
            exchangeRate: '',
            status: '1'
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        codeList: [],
        editModel: { // 编辑视图
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '创建业务参数', // 标题
            type: 'add',
            form: {
                sprCode: '',
                sprName: '',
                sprValue: '',
                sprSortWeights: '',
                sprDesc: '',
                isEnabled: ''
            }
        }
    },
    reducers: {
        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return { ...state, search: action.payload.search };
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = { ...state.editModel };
            if (payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if (payload.title != undefined) {
                newState.title = payload.title;
            }
            if (payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if (payload.type != undefined) {
                newState.type = payload.type;
            }
            if (payload.form != undefined) {
                newState.form = payload.form;
            }

            return { ...state, editModel: { ...newState } };
        },
        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page }
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return { ...state, page: payload.page }
        },
        /**
         * 刷新state
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload }
        }
    },
    effects: {

        /**
         * 分页获取业务参数
         * @param call
         * @param put
         * @param select
         */
        *getBusinessParamsList({ }, { call, put, select }) {
            const search = yield select(state => state.businessParams.search);
            const page = yield select(state => state.businessParams.page);

            const res = yield call(getBusinessParamsList, { sprCode: search.sprCode, page });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 删除业务参数
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteBusinessParamsById({ payload }, { call, put, select }) {
            const res = yield call(deleteBusinessParamsById, { sprCode: payload.sprCode, sprName: payload.sprName });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                yield put({
                    type: "getBusinessParamsList"
                });
            } else {
                message.error(res.data.errMessage);
            }

        },


        /**
         * 获取业务参数详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getBusinessParamsById({ payload }, { call, put, select }) {
            const res = yield call(getBusinessParamsById, { sprCode: payload.sprCode, sprName: payload.sprName });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "修改业务参数",
                        type: 'update',
                        form: {
                            sprCode: content.sprCode,
                            sprName: content.sprName,
                            sprValue: content.sprValue,
                            isEnabled: content.isEnabled,
                            sprDesc: content.sprDesc

                        }
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 修改业务参数
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updateBusinessParams({ payload }, { call, put, select }) {
            const res = yield call(updateBusinessParams, payload);
            if (res.data.resultCode === "ok") {
                message.info("修改成功！重新登录后生效");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getBusinessParamsList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加业务参数
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addBusinessParams({ payload }, { call, put, select }) {
            const res = yield call(addBusinessParams, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getBusinessParamsList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 获取业务参数编码列表
         */
        *getAllBusinessParamCode({ }, { call, put, select }) {

            let codeList = sessionStorage.getItem(Constants.PARAMS_CODE_LIST);

            if (R.isNil(codeList)) {
                const res = yield call(getAllBusinessParamCode);
                codeList = res.data.content;
                if (res.data.resultCode === "ok") {
                    sessionStorage.setItem(Constants.PARAMS_CODE_LIST, JSON.stringify(codeList));//sessionStorage只能存字符串
                    yield put({
                        type: 'refreshState',
                        payload: { codeList }
                    });
                } else {
                    message.error(res.data.errMessage);
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: { codeList: JSON.parse(codeList) }
                });
            }
        }
    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                if (pathname.includes('basedata/businessParams')) {
                    dispatch({
                        type: 'getBusinessParamsList'
                    });
                    dispatch({
                        type: 'getAllBusinessParamCode'
                    });
                }

            });
        }
    }
};
